//34上传附件     111起草合同post
<template>
  <div>
    <el-card class="box-card">
      <h2>起草合同</h2>
      <el-form :model="contractForm" :rules="rules" ref="contractForm" label-width="120px">
        <el-form-item label="合同名称：" prop="name">
          <el-input v-model="contractForm.name"></el-input>
        </el-form-item>
        <el-form-item label="客户：" prop="customer">
          <el-input v-model="contractForm.customer"></el-input>
        </el-form-item>
        <el-form-item label="开始时间：" prop="beginTime">
          <el-date-picker
            v-model="contractForm.beginTime"
            type="date"
            placeholder="选择开始日期"
            :picker-options="beginDatePickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间：" prop="endTime">
          <el-date-picker
            v-model="contractForm.endTime"
            type="date"
            placeholder="选择结束日期"
            :picker-options="endDatePickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="合同内容：" prop="content">
          <el-input v-model="contractForm.content" type="textarea" :rows="10"></el-input>
        </el-form-item>
        <el-form-item label="合同附件：">
          <el-upload
            action="/api/contract/upload"
            list-type="text"
            :on-success="handleUploadSuccess"
            :on-error="handleUploadError"
          >
            <el-button slot="trigger" type="primary">上传附件</el-button>
            <div slot="tip" class="el-upload__tip">上传合同附件，单个文件最大2MB</div>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      contractForm: {
        name: '',
        customer: '',
        beginTime: '',
        endTime: '',
        content: '',
        userName: '', // 可以在创建时设置为当前用户
        attachments: []
      },
      rules: {
        name: [{ required: true, message: '请输入合同名称', trigger: 'blur' }],
        customer: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
        beginTime: [
          { required: true, message: '请选择开始时间', trigger: 'change' },
          { validator: this.validateBeginTime, trigger: 'change' }
        ],
        endTime: [
          { required: true, message: '请选择结束时间', trigger: 'change' },
          { validator: this.validateEndTime, trigger: 'change' }
        ],
        content: [{ required: true, message: '请输入合同内容', trigger: 'blur' }]
      }
    };
  },
  computed: {
    beginDatePickerOptions() {
      return {
        disabledDate: time => {
          return time.getTime() < Date.now() - 86400000; // Disable dates before today
        }
      };
    },
    endDatePickerOptions() {
      return {
        disabledDate: time => {
          return this.contractForm.beginTime
            ? time.getTime() < new Date(this.contractForm.beginTime).getTime()
            : false;
        }
      };
    }
  },
  methods: {
    handleUploadSuccess(response, file, fileList) {
      this.contractForm.attachments.push(response.url);
      this.$message.success(`${file.name} 上传成功`);
    },
    handleUploadError(err, file, fileList) {
      this.$message.error(`${file.name} 上传失败`);
    },
    submitForm() {
      // 设置起草人的用户名，可以从用户信息中获取
      this.contractForm.userName = this.getCurrentUserName();
      this.$refs.contractForm.validate(valid => {
        if (valid) {
          this.axios.post('/api/contract/draft', this.contractForm)
            .then(response => {
              this.$message({
                message: '合同起草成功',
                type: 'success'
              });
              // 重置表单
              this.resetForm();
            })
            .catch(error => {
              console.error('Error creating contract:', error);
              this.$message.error('合同起草失败');
            });
        } else {
          this.$message.error('请完善表单信息');
        }
      });
    },
    getCurrentUserName() {
      // 模拟获取当前用户的方法，实际实现应从认证模块获取
      return '当前用户';
    },
    resetForm() {
      this.$refs.contractForm.resetFields();
      this.contractForm.attachments = [];
    },
    validateBeginTime(rule, value, callback) {
      if (value && new Date(value).getTime() < Date.now() - 86400000) {
        callback(new Error('开始时间不能早于今天'));
      } else {
        callback();
      }
    },
    validateEndTime(rule, value, callback) {
      if (value && new Date(value).getTime() < new Date(this.contractForm.beginTime).getTime()) {
        callback(new Error('结束时间不能早于开始时间'));
      } else {
        callback();
      }
    }
  }
};
</script>

<style scoped>
.box-card {
  margin: auto;
  width: 600px;
}
.el-form-item {
  margin-bottom: 20px;
}
</style>
